<template>
  <div :style="item.type === 'datePicker'?{marginTop: '10px'}:''" class="filter-item">
    <div class="filter-item-name">{{ item.name }}</div>
    <template v-if="item.type === 'checkbox'">
      <el-checkbox-group v-model="dataList" class="filter-checkbox-group" @change="handleCheckedChange">
        <div>
          <span v-if="dataList.length === 0" class="check-box-btn active"><span class="check-box-style"/> <span style="width:120px;">全部</span></span>
          <span v-else class="check-box-btn" @click.stop="cancelSelect(item)"><span class="check-box-style"/> <span style="width:120px;">全部</span></span>
        </div>
        <template v-for="(list, index) in item.value">
          <el-checkbox v-if="isOpen ? true : index < limitNum" :label="list.id" :key="index">
            <div class="filter-item-label">
              <ToolTip :content="list.name" class="filter-item-label-text"/>
            </div>
          </el-checkbox>
        </template>
      </el-checkbox-group>
      <span v-if="item.value.length && item.value.length > limitNum" class="extend-btn" @click="tabOpen">
        {{ isOpen ? '收起' : '展开' }} <i :class="{'open': isOpen}" class="el-icon-arrow-down icon"/>
      </span>
    </template>
    <div v-if="item.type === 'datePicker'" class="mengli-filter-date-container">
      <el-date-picker
        v-model="dataList"
        :clearable="true"
        :editable="false"
        class="mengli-filter-date"
        size="small"
        type="daterange"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        value-format="timestamp"
        format="yyyy/MM/dd"
        @change="changeTime"/>
    </div>
  </div>
</template>
<script>
export default {
  name: 'FilterItem',
  props: {
    item: {
      type: Object,
      default: () => {}
    },
    isReset: {
      type: Boolean,
      default: false
    },
    limitNum: {
      type: Number,
      default: 3
    }
  },
  data() {
    return {
      checkAll: true,
      isIndeterminate: false, // checkbox 的不确定状态
      dataList: [],
      isOpen: false
    };
  },
  computed: {
    arrOptions() {
      const temp = [];
      this.item.value.map(i => {
        temp.push(i.label);
      });
      return temp;
    }
  },
  watch: {
    isReset: {
      handler(val) {
        val && (this.dataList = []);
      },
      immediate: true
    }
  },
  methods: {
    tabOpen() {
      this.isOpen = !this.isOpen;
    },
    formatData(val) {
      const obj = {
        [this.item.label]: val
      };
      return obj;
    },
    cancelSelect(item) {
      this.dataList = [];
      this.$emit('postMessage', this.formatData(this.dataList));
    },
    changeTime(e) {
      this.dataList = e;
      this.$emit('postMessage', this.formatData(this.dataList));
    },
    handleCheckedChange(value) {
      const checkedCount = value.length;
      this.checkAll = this.arrOptions.length === 0;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.arrOptions.length;
      this.$emit('postMessage', this.formatData(this.dataList));
    }
  }
};
</script>
<style lang="scss" scoped>
$ActiveColor: #406EFF;
.filter-item {
  display: flex;
  // margin-top: 34px;
  ::v-deep {
    .el-checkbox {
      flex-wrap: wrap;
      display: flex;
      align-items: center;
      margin-right: 10px;
      height: 36px;
      .el-checkbox__input {
        display: flex;
        align-items: center;
      }
      .el-checkbox__label {
        color: #666666;
        line-height: 24px;
        font-size: 14px;
        font-weight: normal;
      }
      &.is-checked .el-checkbox__label {
        color: #666666;
      }
    }
  }
  .filter-checkbox-group {
    flex-wrap: wrap;
    display: flex;
    flex: 1;
  }
}
.extend-btn {
  font-size:14px;
  font-weight:400;
  line-height: 19px;
  color:$ActiveColor;
  cursor: pointer;
  display: flex;
  padding-top: 7px;
  .icon {
    line-height: 19px;
    height: 19px;
    transition: all 0.3s;
    margin-left: 8px;
    &.open {
      transform: rotate(180deg);
    }
  }
}
.checked-all {
  margin-right: 30px!important;
}
.filter-item-name {
  width: 74px;
  font-family:PingFangSC-Regular,PingFang SC;
  font-weight:400;
  color:#999;
  line-height:1.2;
  font-size: 14px;
  white-space: nowrap;
  margin-right: 12px;
  height: 36px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.mengli-filter-date-container {
  height: 36px;
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  .mengli-filter-date {
    width: 240px;
  }
}
.filter-item-label {
  width: 110px;
  display: flex;
  align-items: center;
  .filter-item-label-text {
    display: inline-block;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }
}
// 复选框
.check-box-btn {
  color: #666666;
  font-size: 14px;
  height: 36px;
  display: flex;
  align-items: center;
  cursor: pointer;
  white-space: nowrap;
  .check-box-style {
    margin-right: 10px;
    display: inline-block;
    position: relative;
    border: 1px solid #DCDFE6;
    border-radius: 2px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    width: 14px;
    height: 14px;
    background-color: #FFFFFF;
    z-index: 1;
    -webkit-transition: border-color 0.25s cubic-bezier(0.71, -0.46, 0.29, 1.46), background-color 0.25s cubic-bezier(0.71, -0.46, 0.29, 1.46);
    transition: border-color 0.25s cubic-bezier(0.71, -0.46, 0.29, 1.46), background-color 0.25s cubic-bezier(0.71, -0.46, 0.29, 1.46);
  }
  &.active {
    // color: $ActiveColor;
    .check-box-style {
      background-color: $ActiveColor;
      border: 1px solid $ActiveColor;
      &::after {
        box-sizing: content-box;
        content: "";
        border: 1px solid #fff;
        border-left: 0;
        border-top: 0;
        height: 7px;
        left: 4px;
        position: absolute;
        top: 1px;
        transform: rotate(45deg);
        width: 3px;
        -webkit-transform-origin: center;
        transform-origin: center;
      }
    }
  }
}
</style>
